from pygame import draw

from pygame.rect import Rect

from libs.ZAnimate import ZAnimate
from libs.ZDisplay import ZDisplay
from config import player_speed , win_size
from libs.fun import cut_image , create_image_alpha


class ZPlayer ( ZDisplay ) :
    def __init__ ( self , father ) :
        super ( ZPlayer , self ).__init__ ( father , [ 68 , 104 ] )
        self.area = Rect ( [ (self.rect.left + 16) , self.rect.top + self._size [ 1 ] - 32 ] , [ 32 , 32 ] )
        self.animates = { }
        master = cut_image ( create_image_alpha ( "assets/image/char/2n.png" ) , 68 , 104 )
        image_list_down = [ master [ 0 ] , master [ 1 ] , master [ 2 ] , master [ 3 ] ]
        image_list_left = [ master [ 4 ] , master [ 5 ] , master [ 6 ] , master [ 7 ] ]
        image_list_right = [ master [ 8 ] , master [ 9 ] , master [ 10 ] , master [ 11 ] ]
        image_list_up = [ master [ 12 ] , master [ 13 ] , master [ 14 ] , master [ 15 ] ]
        self.animates [ 'move_left' ] = ZAnimate ( self , image_list_left , .1 , 0 )
        self.animates [ 'move_right' ] = ZAnimate ( self , image_list_right , .1 , 0 )
        self.animates [ 'move_up' ] = ZAnimate ( self , image_list_up , .1 , 0 )
        self.animates [ 'move_down' ] = ZAnimate ( self , image_list_down , .1 , 0 )
        # self._pos = (win_size [ 0 ] - 30) / 2 , (win_size [ 1 ] - 30) / 2
        self._pos = 1 , 1
        self.walls = [ ]
        self.cache = self.area.copy ( )
        self.testcollide = Rect ( [ 60 , 60 , 32 , 32 ] )
        for x in self.father.walls :
            for y in x.walls :
                self.walls.append ( y )
        self.current_animate = self.animates [ 'move_right' ]

    def cache_move ( self , dire ) :
        if dire == "up" :
            self.cache.move_ip ( 0 , -player_speed )
        if dire == "down" :
            self.cache.move_ip ( 0 , player_speed )
        if dire == "left" :
            self.cache.move_ip ( -player_speed , 0 )
        if dire == "right" :
            self.cache.move_ip ( player_speed , 0 )

    def move_up ( self ) :
        if self.area.top > 0 :
            self.cache_move ( "up" )
        if not self.collect_walls ( ) :
            if self.area.top > 0 :
                self.move ( )
        else :
            self.reset ( )
        self.current_animate = self.animates [ 'move_up' ]

    def move_down ( self ) :
        if self.area.top < win_size [ 1 ] - self.area.height :
            self.cache_move ( "down" )
        if not self.collect_walls ( ) :
            if self.area.top <= win_size [ 1 ] - self.area.height :
                self.move ( )
        else :
            self.reset ( )
        self.current_animate = self.animates [ 'move_down' ]

    def move_left ( self ) :
        self.current_animate = self.animates [ 'move_left' ]
        if self.area.left > 0 :
            self.cache_move ( "left" )
        if not self.collect_walls ( ) :
            if self.area.left > 0 :
                self.move ( )
        else :
            self.reset ( )

    def move_right ( self ) :
        self.current_animate = self.animates [ 'move_right' ]
        if self.area.left < win_size [ 0 ] - self.area.width :
            self.cache_move ( "right" )
        if not self.collect_walls ( ) :
            if self.area.left < win_size [ 0 ] - self.area.width :
                self.move ( )
        else :
            self.reset ( )

    def collect_walls ( self ) :
        collide = self.cache.collidelist ( self.walls )
        if collide == -1 or not collide :
            return False
        else :
            return True

    def draw ( self ) :
        self.current_animate.draw ( )
        # draw.rect ( self.father.image , [ 200 , 200 , 60 ] , self.area )
        # draw.rect ( self.father.image , [ 200 , 100 , 60 ] , self.cache )
        super ( ZPlayer , self ).draw ( )

    def reset ( self ) :
        self.cache.clamp_ip ( self.area )

    def move ( self ) :
        self.area.clamp_ip ( self.cache )
        self._pos = (self.area.left - 16) , self.area.top - self._size [ 1 ] + 32
